package io.yizoo.business.mapper;

import java.util.List;
import java.util.Map;

public class biz_pmis_pjMapperProvider {

    public String GetFullData(Map<String,Object> factor){
        StringBuilder sqlConditionsBuilder = new StringBuilder();
        sqlConditionsBuilder.append(" ").append("where 1=1");
        for (String condition : (List<String>) factor.get("mapper_conditions") ) {
            sqlConditionsBuilder.append(" ").append(condition);
        }

        StringBuilder sqlBuilder = new StringBuilder();
        sqlBuilder.append("WITH RECURSIVE sys_org_path AS (\n" +
                "                            SELECT *,CAST(name AS CHAR(255)) AS path,0 level FROM sys_org\n" +
                "                            UNION ALL\n" +
                "                            SELECT c.*,CONCAT(ct.path, '/', c.name) AS path,level+1 level FROM sys_org c\n" +
                "                            INNER JOIN sys_org_path ct ON c.pid = ct.id\n" +
                "                        ),\n" +
                "\t\t\t\t\t\t\tbiz_pmis_pj_type_path AS (\n" +
                "                            SELECT *,CAST(name AS CHAR(255)) AS path,0 level FROM biz_pmis_pj_type\n" +
                "                            UNION ALL\n" +
                "                            SELECT c.*,CONCAT(ct.path, '/', c.name) AS path,level+1 level FROM biz_pmis_pj_type c\n" +
                "                            INNER JOIN biz_pmis_pj_type_path ct ON c.pid = ct.id\n" +
                "                        ),\n" +
                "\n" +
                "sys_org_paths AS (\n" +
                "    SELECT  *, ROW_NUMBER() OVER (PARTITION BY id ORDER BY level DESC) AS rn FROM  sys_org_path\n" +
                "),\n" +
                "biz_pmis_pj_type_paths AS (\n" +
                "    SELECT  *,ROW_NUMBER() OVER (PARTITION BY id ORDER BY level DESC) AS rn FROM biz_pmis_pj_type_path\n" +
                "),\n" +
                "sys_org_path1s AS (\n" +
                "    SELECT  * FROM  sys_org_paths WHERE rn = 1\n" +
                "),\n" +
                "biz_pmis_pj_type_path1s AS (\n" +
                "    SELECT  * FROM  biz_pmis_pj_type_paths WHERE rn = 1\n" +
                ")\n" +
                "\n" +
                "SELECT  t.*,a.path as org_mc,b.path as type_mc, ifnull(c.name,t.manager) as manager_mc, ifnull(d.name,t.saler) as saler_mc, ifnull(e.name,t.booker) as booker_mc FROM  biz_pmis_pj t\n" +
                "left join sys_org_path1s a on a.id=t.org\n" +
                "left join biz_pmis_pj_type_path1s b on b.id=t.type\n" +
                "left join biz_hrm_ei_basic c on c.emp_code=t.manager\n" +
                "left join biz_hrm_ei_basic d on d.emp_code=t.saler\n" +
                "left join biz_hrm_ei_basic e on e.emp_code=t.booker\n" +
                sqlConditionsBuilder.toString()+
                "\n");
        return sqlBuilder.toString();
    }
    public String Initpj_ins_stage(Map<String,Object> factor) {
        StringBuilder sqlBuilder = new StringBuilder();
        sqlBuilder.append("insert into biz_pmis_pj_ins_stage(id,stage_id,name,sort,pid,edit_page,important,pj_id) " +
                "select UUID(),t.id,t.name,t.sort,t.pid,t.edit_page,t.important,a.id as pj_id from biz_pmis_pj_type_stage t " +
                "left join biz_pmis_pj a on a.type=t.pj_type_id  " +
                "where a.id=#{pj_id}" +
                "");
        return sqlBuilder.toString();
    }

}
